﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundTransfer;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using OnlineVideos.Library;
using Common.Library;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using OnlineVideos.UI;

namespace OnlineVideos
{
    public partial class Downloding : PhoneApplicationPage
    {
        List<DownloadStatus> download = new List<DownloadStatus>();
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);

        string downloadurl = "";
        public Downloding()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txttitle.Text != "")
                {
                    txturl.Text = "http://o-o---preferred---sn-cx5h-itql---v7---lscache3.c.youtube.com/videoplayback?upn=Y8KAuR2NNsM&sparams=cp%2Cgcr%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=901463%2C922401%2C920704%2C912806%2C927201%2C922403%2C925003%2C913546%2C913556%2C916805%2C920201%2C901451&ms=au&expire=1352469373&itag=22&ipbits=8&gcr=in&sver=3&ratebypass=yes&mt=1352444472&ip=183.82.195.133&mv=m&source=youtube&key=yt1&cp=U0hUR1RRV19MTUNONF9MS0FEOmwxODFNOFhXVGlE&id=9eff4dc0a0239add&newshard=yes&signature=D6FCB6F8B2380F8B7B128100D474415266F39E1A.95BF4057D7C04F3ED557C6B23E68BC4BAA0CDD3E&title=Obama&#39;s%20Complete%20Victory%20Speech:%20Obama%20Wins%20the%202012%20Election";
                    DownloadManager.SaveDownloadingInfo(txttitle.Text, txturl.Text);
                    AppSettings.DownloadTitle = txttitle.Text;
                    if (BackgroundTransferService.Requests.Count() >= 5)
                    {

                        MessageBox.Show("The maximum number of background file transfer requests for this application has been exceeded. ");
                        return;
                    }

                    string transferFileName = txturl.Text;

                    //string transferFileName = "http://lartsoft.org/ActivityTrakerHelpVideos/Howtoviewtimespentchart.wmv";
                    Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);
                    BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
                    transferRequest.Method = "GET";
                    string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
                    AppSettings.DownloadFile = downloadFile;

                    Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute);
                    transferRequest.DownloadLocation = downloadUri;
                    string filepath = downloadUri.ToString();
                    transferRequest.Tag = downloadFile;
                    DownloadManager.DownloadingStatus(filepath, "", transferRequest.TotalBytesToReceive);
                    //DownloadManager.DeleteDownloadUrls(filepath);
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    try
                    {
                        BackgroundTransferService.Add(transferRequest);
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show("Unable to add background transfer request. " + ex.Message);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Unable to add background transfer request.");
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in button1_Click event In downloadingpage file.", ex);
            }
        }
        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("Downloding Page", true);
                string url = "";
                if (NavigationContext.QueryString.TryGetValue("url", out url))
                {
                    downloadurl = url;
                    txturl.Text = downloadurl;
                }
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In Downloding.cs file.", ex);
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("Downloding Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In Downloding.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In Downloding.cs file.", ex);
            }
        }

    }
}